home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / platforms and tools / project builder / simon / simoncontroller.m < prev   
Encoding:
Text File  |  2000-10-06  |  5.4 KB  |  152 lines

  1. /*
  2.     File:        SimonController.m    
  3.  
  4.     Contains:    A sample of a simple Cocoa Application
  5.  
  6.     Written by:     Karl Groethe
  7.  
  8.     Copyright:    Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.             You may incorporate this Apple sample source code into your program(s) without
  11.             restriction. This Apple sample source code has been provided "AS IS" and the
  12.             responsibility for its operation is yours. You are not permitted to redistribute
  13.             this Apple sample source code as "Apple sample source code" after having made
  14.             changes. If you're going to re-distribute the source, we require that you make
  15.             it clear in the source that the code was descended from Apple sample source
  16.             code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                         6/00     created
  20.                 
  21.  
  22. */
  23. #import "SimonController.h"
  24.  
  25. #define HALF_A_SEC 30
  26. #define ONE_SEC    60
  27. #define TWO_SEC 120
  28. #define NUM_SIMON_BUTTON 4
  29. #define BUTTON_ONE 0
  30. #define BUTTON_TWO 1
  31. #define BUTTON_THREE 2
  32. #define BUTTON_FOUR 3
  33. #define START_STR "Start"
  34. #define STOP_STR  "Stop"
  35. #define PRESS_START_STR "Press start to begin."
  36. #define PLAYING_STR    "Remember the sequence..."
  37. #define LISTENING_STR    "Repeat the sequence..."
  38. #define GAME_OVER    "Incorrect Sequence. Game Over"
  39.  
  40. @implementation SimonController
  41. - (id)init
  42. {
  43.     /*------------------------------------------------------
  44.         initialize controller members
  45.     --------------------------------------------------------*/
  46.     self=[super init];//chain up to superclass
  47.     
  48.     listening=FALSE;
  49.     sequenceLength=0;
  50.     
  51.     return self;
  52. }
  53. - (void)StartStopGame:(id)sender
  54. {
  55.      /*------------------------------------------------------
  56.         Handle starting and stopping the game
  57.     --------------------------------------------------------*/
  58.  
  59.     static int playing=FALSE;
  60.     
  61.     if(playing==TRUE){
  62.         playing=FALSE;            //we're playing so stop playing
  63.         listening=FALSE;        //stop listening as well
  64.         //change the title of the Start/Stop button
  65.         [myStartStop setTitle:[NSString stringWithCString:START_STR]];
  66.         [myStartStop display];        //refresh title of the Start/Stop button
  67.         //change message string to reflect that we are not playing 
  68.         [myMessage setStringValue:[NSString stringWithCString:PRESS_START_STR]];
  69.         [myMessage display];        //refresh message on screen
  70.         sequenceLength=0;        //reset sequence
  71.     }else{
  72.         playing=TRUE;            //we're not playing so start playing
  73.         [myCounter setIntValue:0];    //reset counter
  74.         [myCounter display];        //refresh counter on screen
  75.                                         //change the title of the Start/Stop button
  76.         [myStartStop setTitle:[NSString stringWithCString:STOP_STR]];
  77.         [myStartStop display];        //refresh button
  78.         //change message string to reflect that we are now playing
  79.         [myMessage   setStringValue:[NSString stringWithCString:PLAYING_STR]];
  80.         [myMessage display];        //refresh message on screen
  81.         [self PlaySequence];        //start playing a new sequence
  82.     }
  83. }
  84. - (void)ListenSequence:(id)sender
  85. {
  86.     /*------------------------------------------------------
  87.         Listen for the user's sequence and evaluate it
  88.     --------------------------------------------------------*/
  89.     static int index=0;//the element we are currently looking at
  90.     long junk;
  91.     Delay(HALF_A_SEC,&junk);//slow things down a little
  92.     if(listening==TRUE){            //we're listening
  93.             if(sequence[index]==sender){    //check the player's selection
  94.             index++;                //correct selection so move on
  95.             [myCounter setIntValue:index];    //update the onscreen counter
  96.             [myCounter display];        
  97.             if(index==sequenceLength){        //end of the sequence?
  98.                 [sender highlight:NO];        //reset hilighting
  99.                 [sender display];
  100.                 [self PlaySequence];        //play the new sequence
  101.                 index=0;
  102.             }
  103.         }else{                    //wrong selection so game over
  104.             [myMessage setStringValue:[NSString stringWithCString:GAME_OVER]];
  105.             [myMessage display];
  106.             [sender highlight:NO];
  107.             [sender display];
  108.             Delay(TWO_SEC,&junk);        //let it sink in
  109.             [self StartStopGame:self];        //stop the game
  110.         }
  111.     }else
  112.         index=0;
  113. }
  114.  
  115. - (void)PlaySequence
  116. {
  117.     /*------------------------------------------------------
  118.         Play a new sequence for the player to remember
  119.     --------------------------------------------------------*/
  120.     int i=0;
  121.     long junk;
  122.     listening=FALSE;                //stop listening
  123.     srand(time(NULL));                //randomize
  124.     switch(rand()%NUM_SIMON_BUTTON){        //pick a random button
  125.         case BUTTON_ONE:
  126.             sequence[sequenceLength++]=myButton1;//and add it to the sequence
  127.             break;
  128.         case BUTTON_TWO:
  129.             sequence[sequenceLength++]=myButton2;
  130.             break;
  131.         case BUTTON_THREE:
  132.             sequence[sequenceLength++]=myButton3;
  133.             break;
  134.         case BUTTON_FOUR:
  135.             sequence[sequenceLength++]=myButton4;
  136.             break;
  137.     }
  138.     [myMessage setStringValue:[NSString stringWithCString:PLAYING_STR]];
  139.     [myMessage display];
  140.     for(i=0;i<sequenceLength;i++){    //play the sequence
  141.         Delay(HALF_A_SEC,&junk);
  142.         [myCounter setIntValue:i+1];
  143.         [myCounter display];
  144.         [sequence[i] performClick:self];//simulate button click
  145.     }
  146.     listening=TRUE;            //start listening again
  147.     [myMessage setStringValue:[NSString stringWithCString:LISTENING_STR]];
  148.     [myMessage display];
  149.     
  150. }
  151. @end
  152.